# -*- coding: utf-8 -*-
"""
Created on Wed Mar  6 15:54:12 2024

@author: heyuesong
"""

import requests
import requests
import pandas as pd
from utils import general

js = general.read_js(r'D:\code\猿人学\第三题.js')


result_list = []
for i in range(1,101):
    print(i)
    m = general.requests_re('=(.*?);',js.call('get_cookie'))
    headers = {
        "authority": "www.python-spider.com",
        "accept": "application/json, text/javascript, */*; q=0.01",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "origin": "https://www.python-spider.com",
        "pragma": "no-cache",
        "referer": "https://www.python-spider.com/challenge/3",
        "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
        "x-requested-with": "XMLHttpRequest"
    }
    cookies = {
        "sessionid": "4vf6v1ug1qwmubnqzmazenphmbaaisxa",
        "Hm_lvt_337e99a01a907a08d00bed4a1a52e35d": "1709706135,1709706247",
        "m": m,
        "Hm_lpvt_337e99a01a907a08d00bed4a1a52e35d": "1709712112"
    }
    url = "https://www.python-spider.com/api/challenge3"
    data = {
        "page": f"{i}"
    }
    response = requests.post(url, headers=headers, cookies=cookies, data=data)
    html = response.json()['data']
    result_list+=html
result_df = pd.DataFrame(result_list)

result_df['value'] = result_df['value'].apply(lambda x:int(x.replace('\r','')))


print(sum(result_df['value']))


